<template>
	<view class="wrap">
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
			</view>
		</view>
		<view class="gift-list" v-if="state.items.length > 0">
			<view class="b-item" v-for="(item, index) in state.items" :key="index">
				<view class="b-head">
					<view class="s-order">订单编号：{{item.number}}</view>
					<view class="s-status" :class="stateStyle(item.status)">{{item.status_text}}</view>
				</view>
				<view class="b-gift" @click="onRecordDetail(item.id)">
					<view class="b-left">
						<image :src="item.gift_data.cover_url" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">{{item.gift_data.name}}</view>
						<view class="b-spec" v-if="item.gift_data.spec_data">
							<view class="s-tit">规格:</view>
							<view class="s-value">{{item.gift_data.spec_data.name}}</view>
						</view>
						<view class="b-fund">
							<view class="s-worth" v-if="item.gift_data.order_worth">价值 {{item.gift_data.order_worth}}元
							</view>
						</view>
					</view>
				</view>
				<view class="b-foot">
					<view class="s-time">兑换时间：{{timeFormat(item.created_at)}}</view>
				</view>
				<view class="b-action">
					<view class="b-btn" @click="onRecordDetail(item.id)">订单详情</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" color="#888888"></load-more>
		<view class="navbar">
			<view class="b-action">
				<view class="s-home" @click="onHome()">回到首页</view>
			</view>
		</view>
		<!-- 在线客服 -->
		<view class="customer-service" @click="onCustomerService">
			<image src="/static/imgs/kf.png" mode="widthFix"></image>
			<text>联系客服</text>
		</view>
	</view>
</template>

<script>
	import {
		getOrderList
	} from '@/service/api/card.js'
	import Util from '@/common/util'
	import mixinsCommon from '@/mixins/common.js'
	import mixinsAuth from '@/mixins/cardAuth.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				hasLoad: false,
				refreshTime: 0,
				groupIndex: 0,
				groupList: [{
						name: '全部',
						value: 0
					},
					{
						name: '待发货',
						value: 1
					},
					{
						name: '已发货',
						value: 2
					},
					{
						name: '已取消',
						value: 3
					},
				],
				state: {
					items: []
				},
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadOrderList(true)
		},
		onShow() {
			if (this.hasLoad) {
				const timestamp = Util.getTimestamp()
				if (timestamp - this.refreshTime >= 10) {
					this.refreshTime = timestamp
					this.loadOrderList(true)
				}
			}
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadOrderList();
			}
		},
		computed: {
			stateStyle() {
				return (status) => {
					return {
						'f-wait': status === 1,
						'f-done': status === 2,
						'f-cancel': status === 3,
					}
				}
			},
		},
		methods: {
			loadOrderList(refresh) {
				console.log('loadOrderList', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getOrderList(this.groupList[this.groupIndex].value, this.page, 10).then(([err, res]) => {
					console.log('getList', err, res)
					this.loadingType = -1
					this.hasLoad = true
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无订单'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onGroupSelect(index) {
				this.groupIndex = index
				this.loadOrderList(true)
			},
			onRecordDetail(recordId) {
				uni.navigateTo({
					url: '/pages/card/order_detail?id=' + recordId
				})
			},
			onHome() {
				uni.navigateTo({
					url: '/pages/card/index'
				})
			},
			onCustomerService() {
				// 打开有赞客服链接
				const serviceUrl = 'https://shop93244668.youzan.com/v3/im/index?c=wsc&v=2&o=https%3A%2F%2Fshop93244668.youzan.com%2F&kdt_id=93052500&type=default&fromSource=%7B%22kdt_id%22%3A%2293052500%22%2C%22source%22%3A%22default%22%2C%22endpoint%22%3A%22h5%22%2C%22site_id%22%3A0%7D&reft=1734428093773&spm=f.91451403#/index'
				//const serviceUrl = 'https://work.weixin.qq.com/kfid/kfce412dbdd0a94f8f3'
				// #ifdef H5
				window.location.href = serviceUrl
				// #endif
				// #ifdef MP-WEIXIN
				uni.navigateTo({
					url: '/pages/webview/index?url=' + encodeURIComponent(serviceUrl)
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding-bottom: 150upx;
	}

	.filter-box {
		//padding: 30upx 50upx 0 50upx;
		margin-bottom: 30upx;
		background: #fff;
		// border-bottom: 1upx solid #eee;
		display: flex;
		align-items: center;

		.b-item {
			margin: 0 40upx;
			height: 80upx;
			line-height: 80upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;
			padding-bottom: 6upx;

			&.f-active {
				padding-bottom: 0;
				border-bottom: 6upx solid #DA5650;

				.s-tit {
					color: #DA5650;
				}
			}

			.s-tit {
				color: #333;
			}
		}
	}

	.gift-list {
		.b-item {
			background: #fff;
			margin-bottom: 30upx;

			&:last-child {
				margin-bottom: 0;
			}

			.b-head {
				padding: 20upx 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1upx solid #eee;

				.s-order {
					color: #666;
					font-size: 26upx;
				}

				.s-status {
					font-size: 28upx;

					&.f-wait {
						color: #FFC300;
					}

					&.f-done {
						color: #DA5650;
					}

					&.f-cancel {
						color: #666;
					}
				}
			}

			.b-gift {
				padding: 30upx;
				display: flex;
				border-bottom: 1upx solid #eee;

				.b-left {
					width: 160upx;
					height: 160upx;

					image {
						width: 160upx;
						height: 160upx;
					}
				}

				.b-main {
					margin-left: 30upx;
					flex: 1;
					display: flex;
					flex-direction: column;

					.b-title {
						font-size: 28upx;
						color: #333;
					}


					.b-spec {
						margin-top: 20upx;
						display: flex;
						align-items: center;

						.s-tit {
							font-size: 24upx;
							color: #999;
						}

						.s-value {
							margin-left: 10upx;
							font-size: 24upx;
							color: #666;
						}
					}

					.b-fund {
						margin-top: 20upx;
						display: flex;
						align-items: baseline;

						.s-worth {
							font-size: 24upx;
							color: #999;
						}
					}
				}
			}

			.b-foot {
				padding: 20upx 30upx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.s-time {
					color: #999;
					font-size: 26upx;
				}
			}


			.b-action {
				padding: 20upx 30upx 40upx 30upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.b-btn {
					padding: 0 20upx;
					border: 1upx solid #bbb;
					color: #666;
					font-size: 26upx;
					line-height: 60upx;
					height: 60upx;
					border-radius: 10upx;
				}
			}
		}
	}

	.navbar {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 100upx;
		background: #fff;
		// border-top: 1upx solid #f6f6f6;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.b-action {
			flex: 1;

			.s-home {
				background: #DA5650;
				height: 100upx;
				padding: 0 60upx;
				line-height: 100upx;
				font-size: 30upx;
				color: #fff;
				letter-spacing: 4upx;
				text-align: center;
			}
		}
	}
	.customer-service {
		position: fixed;
		right: 30upx;
		bottom: 120upx;
		z-index: 99;
		text-align: center;
		
		image {
			width: 100upx;
			height: 100upx;
		}
		
		text {
			display: block;
			font-size: 24upx;
			color: #666;
			margin-top: 4upx;
		}
	}
</style>
